package jzOffer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 郝文雪
 * @date 2022/8/2 20:27
 * @Description Test
 */
public class JZ13 {
    public int movingCount(int threshold, int rows, int cols) {
        Queue<int[]> q = new LinkedList<>();
        boolean[][] visited = new boolean[rows][cols];
        q.offer(new int[]{0, 0, 0, 0});
        int count = 0;
        while (q.size() > 0) {
            int[] x = q.poll();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if (i >= rows || j >= cols || si + sj > threshold || visited[i][j]) continue;
            visited[i][j] = true;
            count++;
            q.offer(new int[]{i + 1, j, (i + 1) % 10 == 0 ? si - 8 : si + 1, sj});
            q.offer(new int[]{i, j + 1, si, (j + 1) % 10 == 0 ? sj - 8 : sj + 1});
        }
        return count;
    }
}

